<template>
    <div class="my-task-detail-box" v-loading="loading">
        <el-form :model="detail.boxConf" :rules="rules" ref="form" label-width="120px" class="form-box input-no-border-box">
            <topTitle title="基本信息"/>
            <!-- <el-form-item label="签到标准" prop="region">
                <el-select v-model="detail.region" placeholder="请选择活动区域">
                    <el-option label="玩游戏时长6分钟" value="shanghai"></el-option>
                    <el-option label="玩游戏时长10分钟" value="beijing"></el-option>
                </el-select>
            </el-form-item> -->
            <el-form-item label="任务标题" prop="title">
                <el-input v-model.trim="detail.boxConf.title" maxlength="10" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="长按宝箱文案" prop="desc">
                <el-input v-model.trim="detail.boxConf.desc" type="textarea" :rows="5"></el-input>
            </el-form-item>

            <topTitle title="奖励规则"/>
            <template v-if="type === '1'">
                <el-form-item label="获得宝箱条件" prop="dayLimit">
                    <el-input v-model.number="detail.boxConf.dayLimit" type="number" style="width:200px;"></el-input>
                    <span style="margin-left:5px;">天</span>
                </el-form-item>

                <el-form-item label="签到标准" prop="playTimeLimit">
                    <el-input v-model.number="detail.boxConf.playTimeLimit" type="number" style="width:200px;"></el-input>
                    <span style="margin-left:5px;">分钟</span>
                </el-form-item>
            </template>

            <el-form-item label="获得宝箱条件" prop="playTimeLimit" v-else>
                <el-input v-model.number="detail.boxConf.playTimeLimit" type="number" style="width:200px;"></el-input>
                <span style="margin-left:5px;">分钟</span>
            </el-form-item>
            <el-form-item label="宝箱成团个数" prop="groupNum">
                <el-input v-model.number="detail.boxConf.groupNum" type="number" style="width:200px;"></el-input>
            </el-form-item>
        </el-form>

        <!-- <div class="table-box">
            <div class="table-title-box">
                <div class="table-title-item">奖励内容</div>
                <div class="table-title-item">单价</div>
                <div class="table-title-item">奖励类型</div>
                <div class="table-title-item">奖励</div>
                <div class="table-title-item">概率</div>
                <div class="table-title-item">操作</div>
            </div>
            <div v-for="(item, index) in detail.boxRewardConfVoList" :key="index" class="table-for-box">
                <div class="table-content-box">
                    <div class="table-content-item">
                        <el-select v-model="item.rewardType">
                            <el-option label="经验" :value="0"></el-option>
                            <el-option label="金币" :value="1"></el-option>
                        </el-select>
                    </div>
                    <div class="table-content-item">
                        <input type="number">
                    </div>
                    <div class="table-content-item">
                        <el-select v-model="item.giveType">
                            <el-option label="固定" :value="0"></el-option>
                            <el-option label="正态分布" :value="1"></el-option>
                        </el-select>
                    </div>
                    <div class="table-content-item">
                        <input type="number" v-model.number="item.num">
                    </div>
                    <div class="table-content-item">
                        <input type="number">
                    </div>
                    <div class="table-content-item">
                        <el-button  @click="delTaskItem(index, detail.boxRewardConfVoList)" type="text" size="small" v-if="detail.boxRewardConfVoList.length > 1">删除</el-button>
                        <el-button type="text" size="small" @click="addItem(index, detail.boxRewardConfVoList)">添加</el-button>
                    </div>
                </div>
                <div class="table-children-box" v-if="item.rewardType === 2">
                    <div style="width:279px;"></div>
                    <div style="flex:1;">
                        <div class="children-title-box">
                            <div class="children-title-item">最小值</div>
                            <div class="children-title-item">最大值</div>
                            <div class="children-title-item">中值</div>
                            <div class="children-title-item">中值占比</div>
                        </div>
                        <div class="children-content-box" v-for="(childrenItem, childrenIndex) in item.children" :key="childrenIndex">
                            <div class="children-content-item">
                                <input type="number" v-model="childrenItem.name">
                            </div>
                            <div class="children-content-item">
                                <input type="number">
                            </div>
                            <div class="children-content-item">
                                <input type="number">
                            </div>
                            <div class="children-content-item">
                                <input type="number">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div> -->

        <div class="submit-box" v-if="changeNum > 1">
            <el-button @click="notSave">取 消</el-button>
            <el-button type="success" @click="beforeSave">保 存</el-button>
        </div>
    </div>
</template>

<script>
export default {
    data () {
        return {
            changeNum: 0,
            loading: false,
            detail: {
                boxConf: {},
                boxRewardConfVoList: []
            },
            rules: {
                title: [
                    { required: true, message: '请输入任务标题', trigger: 'blur' }
                ],
                desc: [
                    { required: true, message: '请输入长按宝箱文案', trigger: 'blur' }
                ],
                playTimeLimit: [
                    { required: true, message: '请输入获得宝箱条件', trigger: 'blur' }
                ],
                dayLimit: [
                    { required: true, message: '请输入获得宝箱条件', trigger: 'blur' }
                ],
                groupNum: [
                    { required: true, message: '请输入宝箱成团个数', trigger: 'change' }
                ]
            }

        }
    },

    props: ['type'],

    watch: {
        detail: {
            handler () {
                this.changeNum++
            },
            deep: true
        }
    },

    created () {
        if (this.type === '1') {
            this.rules.playTimeLimit = [
                { required: true, message: '请输入签到标准', trigger: 'blur' }
            ]
        } else {
            this.rules.playTimeLimit = [
                { required: true, message: '请输入获得宝箱条件', trigger: 'blur' }
            ]
        }
        this.getDetail()
    },

    methods: {
        // 获取数据详情
        async getDetail () {
            this.loading = true
            this.changeNum = 0
            let params = {
                boxType: this.type
            }
            let res = await this.$http.get('/task/findGameTask', {
                params
            })
            if (res.code === '0') {
                res.data.boxConf = res.data.boxConf || {}
                res.data.boxRewardConfVoList = res.data.boxRewardConfVoList || []
                this.detail = res.data
                this.currentDetail = JSON.parse(JSON.stringify(res.data))
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
            this.loading = false
        },

        delTaskItem () {

        },

        addItem () {

        },

        // 取消保存
        notSave () {
            this.changeNum = 0
            this.detail = JSON.parse(JSON.stringify(this.currentDetail))
        },
        beforeSave () {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.save()
                } else {
                    return false
                }
            })
        },

        // 保存
        async save () {
            let res = await this.$http.post('/task/updateGameTask', this.detail)
            if (res.code === '0') {
                this.getDetail()
                this.$message({
                    type: 'success',
                    message: '修改成功'
                })
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
        }
    }
}
</script>

<style lang="scss" scoped>
    .my-task-detail-box {
        .form-box {
            max-width: 800px;
        }

        .table-box {
            margin-top: 20px;
            border: 1px solid #ebeef5;
            border-bottom: none;
            .table-title-box {
                display: flex;
                font-size: 14px;
                color: #909399;
                border-bottom: 1px solid #ebeef5;
                .table-title-item {
                    flex: 1;
                    padding: 10px;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    border-right: 1px solid #ebeef5;
                    &:last-child {
                        border: none;
                    }
                }
            }
            .table-for-box {
                .table-content-box {
                    display: flex;
                    border-bottom: 1px solid #ebeef5;
                    .table-content-item {
                        flex: 1;
                        // min-height: 40px;
                        padding: 10px;
                        font-size: 14px;
                        color: #606266;
                        display: flex;
                        align-items: center;
                        justify-content: space-around;
                        border-right: 1px solid #ebeef5;
                        &:last-child {
                            border: none;
                        }
                        > input {
                            width: 100%;
                            text-align: center;
                        }
                    }
                }
                .table-children-box {
                    display: flex;
                    border-bottom: 1px solid #ebeef5;
                    .children-title-box {
                        display: flex;
                        .children-title-item {
                            flex: 1;
                            font-size: 14px;
                            color: #909399;
                            padding: 10px;
                            display: flex;
                            align-items: center;
                            justify-content: center;
                            border-left: 1px solid #ebeef5;
                        }
                    }
                    .children-content-box {
                        display: flex;
                        border-top: 1px solid #ebeef5;
                        .children-content-item {
                            flex: 1;
                            padding: 10px;
                            font-size: 14px;
                            color: #606266;
                            display: flex;
                            align-items: center;
                            justify-content: space-around;
                            border-left: 1px solid #ebeef5;
                            > input {
                                width: 100%;
                                text-align: center;
                            }
                        }
                    }
                }
            }
        }

        .submit-box {
            width: 300px;
            margin: 0 auto;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
    }
</style>
